import * as React from "react"
import { type SearchParams } from "@/types/table"

import { getValidFilters } from "@/lib/data-table"
import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton"
import { shipbuildingSearchParamsCache, offshoreTopSearchParamsCache, offshoreHullSearchParamsCache } from "@/lib/items-tech/validations"
import { getShipbuildingItems, getOffshoreTopItems, getOffshoreHullItems } from "@/lib/items-tech/service"
import { OffshoreTopTable } from "@/lib/items-tech/table/top/offshore-top-table"
import { OffshoreHullTable } from "@/lib/items-tech/table/hull/offshore-hull-table"

// 대소문자 문제 해결 - 실제 파일명에 맞게 import
import { ItemsShipTable } from "@/lib/items-tech/table/ship/Items-ship-table"

interface IndexPageProps {
  searchParams: Promise<SearchParams>
}

export default async function IndexPage({ searchParams }: IndexPageProps) {
  const params = await searchParams
  const shipbuildingSearch = shipbuildingSearchParamsCache.parse(params)
  const offshoreTopSearch = offshoreTopSearchParamsCache.parse(params)
  const offshoreHullSearch = offshoreHullSearchParamsCache.parse(params)
  const validShipbuildingFilters = getValidFilters(shipbuildingSearch.filters || [])
  const validOffshoreTopFilters = getValidFilters(offshoreTopSearch.filters || [])
  const validOffshoreHullFilters = getValidFilters(offshoreHullSearch.filters || [])

  
  // URL에서 아이템 타입 가져오기
  const itemType = params.type || "ship"

  return (
    <div>
      {itemType === "ship" && (
        <ItemsShipTable 
          promises={Promise.all([
            getShipbuildingItems({
              ...shipbuildingSearch,
              filters: validShipbuildingFilters,
            }),
          ]).then(([result]) => result)} 
        />
      )}
      
      {itemType === "top" && (
        <OffshoreTopTable 
          promises={Promise.all([
            getOffshoreTopItems({
              ...offshoreTopSearch,  
              filters: validOffshoreTopFilters,
            }),
          ]).then(([result]) => result)} 
        />
      )}
      
      {itemType === "hull" && (
        <OffshoreHullTable 
          promises={Promise.all([
            getOffshoreHullItems({
              ...offshoreHullSearch,
              filters: validOffshoreHullFilters,
            }),
          ]).then(([result]) => result)} 
        />
      )}
    </div>
  )
}
